package com.liangxy.news.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.liangxy.news.model.User;

import java.util.List;

/**
 * DAO for users
 */
@Dao
public interface UserDao {
    
    /**
     * Insert a user
     * @param user User to insert
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(User user);
    
    /**
     * Update a user
     * @param user User to update
     */
    @Update
    void update(User user);
    
    /**
     * Delete user from the database
     * @param user User to delete
     */
    @Delete
    void delete(User user);
    
    /**
     * Get user by ID
     * @param id User ID
     * @return User with the given ID
     */
    @Query("SELECT * FROM users WHERE id = :id")
    LiveData<User> getUserById(String id);
    
    /**
     * Get user by phone
     * @param phone User phone
     * @return User with the given phone
     */
    @Query("SELECT * FROM users WHERE phone = :phone")
    User getUserByPhone(String phone);
    
    /**
     * Get user by phone number
     * @param phoneNumber User phone number
     * @return User with the given phone number
     */
    @Query("SELECT * FROM users WHERE phone = :phoneNumber")
    User getUserByPhoneNumber(String phoneNumber);
    
    /**
     * Delete user by ID
     * @param id User ID
     */
    @Query("DELETE FROM users WHERE id = :id")
    void deleteUserById(String id);
    
    /**
     * Get all users from the database
     * @return List of all users
     */
    @Query("SELECT * FROM users")
    List<User> getAllUsers();
    
    /**
     * Update user password
     * @param phoneNumber User phone number
     * @param password New password
     */
    @Query("UPDATE users SET password = :password WHERE phone = :phoneNumber")
    void updateUserPassword(String phoneNumber, String password);
    
    /**
     * Delete all users
     */
    @Query("DELETE FROM users")
    void deleteAll();
    
    /**
     * Get user by ID synchronously
     * @param id User ID
     * @return User with the given ID
     */
    @Query("SELECT * FROM users WHERE id = :id")
    User getUserByIdSync(String id);
} 